package com.smcdd.cdb.dbo.cdb_dbo.service.presenter;

import android.util.Log;

import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Charging;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.DataManager;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.ICdbListInfoAc;
import com.smcdd.cdb.dbo.cdb_dbo.ui.adapter.CdbListAdapter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.adapter.YetAdapter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;

import java.util.List;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * 作者：Administrator on 2017/6/22 14:11
 * 邮箱：zhanghuaiha@gmail.com
 */

public class CdbListInfoPresenter extends BasePresenter<ICdbListInfoAc> {

    private CdbListAdapter adapter;
    private DataManager manager;
    private CompositeSubscription mCompositeSubscription;

    public CdbListInfoPresenter(BaseActivity context) {
        super(context);
    }

    public void loadContacts(String id,String url){
        manager = new DataManager(mContext);
        mCompositeSubscription = new CompositeSubscription();
        setAdapter(url);
        loadData(id);
    }

    public void unLoadData(){
        if (mCompositeSubscription.hasSubscriptions()){
            mCompositeSubscription.unsubscribe();
        }
    }

    private void setAdapter(String url) {
        if (adapter == null) {
            adapter = new CdbListAdapter(mContext,url);
        }
        getView().getRecycler().setAdapter(adapter);
        adapter.setItemClickListenter((dataBean, position) -> Log.e("aaa","aaaaaaaaaaaaa"));
    }

    private void loadData(String id) {
        String udid = App.SP.getString("udid", "");
        String time = System.currentTimeMillis() + "";
        String eid = App.SP.getString("id", "");
        String versionName = ObtainVersion.getVersionName(mContext);
        String encry = ObtainEncry.getCharging2Page("2", time, versionName, NetWorkCodeInfo.CHARGING_PAGE, udid, id, eid, "1000", "0");
        mCompositeSubscription.add(manager.getCharging2Page("2",time,versionName, NetWorkCodeInfo.CHARGING_PAGE,udid,id,eid,"1000","0",encry)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<Charging>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Charging charging) {
                Log.e("CdbListInfoPresenter",charging.getCode()+"");
                if (charging.getCode() == 200) {
                    List<Charging.DataBean> data = charging.getData();
                    adapter.addData(data);
                }
            }
        }));

    }
}
